package com.xhwl.logistics.service.impl.http;


import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.exception.ApplicationException;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.dto.http.CarDispatchMapDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 对接车辆调度的接口
 */
@Slf4j
//@Service
public class CarDispatchHttpServiceImpl {

    /**
     * 车辆调度的接口，根据车牌号批量获取车辆类型
     */
    private static final String carTypeByCarNum = "/vss/truck/getOrderType";
    private static final String successCode = "200";

    /**
     * 根据车牌号批量获取车辆类型
     */
    public List<CarDispatchMapDTO> getCarTypeByCarNum(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, List<String> plateNumbers){
        HashMap<String, Object> params = new HashMap<>();
        params.put("truckNoList", plateNumbers);
        JSONObject resuleObject = post(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getHostThree() + carTypeByCarNum, params);
        List<CarDispatchMapDTO> carDispatchMapDTOS = JSONObject.parseArray(carDispatchResponseHandler(resuleObject), CarDispatchMapDTO.class);
        return carDispatchMapDTOS.stream().map(carDispatchMapDTO -> {
            carDispatchMapDTO.setValue(carOrdertoCarType(carDispatchMapDTO.getValue()));
            return carDispatchMapDTO;
        }).collect(Collectors.toList());
    }

    /**
     * 将车辆调度的车辆订单转换为车辆类型
     */
    private String carOrdertoCarType(String carType){
        switch (carType){
            case "SALES_ORDER":
                return "成品车";
            case "PURCHASE_ORDER":
                return "原辅料车";
            case "MILK_IN_ORDER":
                return "奶车";
            case "XNY_OUT_ORDER":
                return "稀奶油车";
            case "MILK_OUT_ORDER":
                return "原奶外送车";
            case "TRAIN_TRUCK":
                return "火车倒运车";
            case "PURCHASE_ORDER_TANK":
                return "槽车";
            default:
                return "非货车";
        }
    }


    /**
     * 对车辆调度厂商返回的结果进行判断
     */
    private String carDispatchResponseHandler(JSONObject resultObject){
        String code = resultObject.getString("code");
        if(!StringUtils.equals(code, successCode)){
            throw new BusinessException(resultObject.getString("msg"));
        }
        return resultObject.getString("dataList");
    }

    /**
     * 请求车帘调度第三方的接口
     */
    private JSONObject post(String path, Map<String, Object> params){
        log.info("【CarDispatch post】path:{},params:{}",path,JSONObject.toJSONString(params));
        String result = null;
        try {
            result = HttpRequest
                    .post(path)
                    .body(JSONObject.toJSONString(params))
                    .execute()
                    .body();
        }catch (Exception e){
            log.error("【CarDispatch post】path:{},params:{};require is failed!",path,JSONObject.toJSONString(params));
            throw new ApplicationException();
        }
        if(StringUtils.isEmpty(result)){
            log.error("【CarDispatch post】path:{},params:{};resule is null！",path,JSONObject.toJSONString(params));
            throw new BusinessException();
        }
        log.info("【CarDispatch post】path:{},params:{},result:{}",path,JSONObject.toJSONString(params),result);
        return JSONObject.parseObject(result);
    }
}
